package il.adscan.scan.json.impl;

public class WinWinScanner2  {

//    @Override
//    protected List<Advertisement> scrapPageData(HtmlPage htmlPage) throws IOException {
//        Document doc = Jsoup.parse(htmlPage.asXml());
//        Elements elements = doc.select("#gridWrapper1 [onclick*=openGridTr]");
//        List<Advertisement> advertisementList = new ArrayList<>();
//        int i = 0;
//        ExecutorService executorService = Executors.newFixedThreadPool(20);
//
//        Set<Callable<Advertisement>> callables = new HashSet<>();
//        List<Future<Advertisement>> advertisementFutureList = new ArrayList<>();
//        for (Element element : elements) {
//            if (advertisementList.size() >= numberOfItemsToScan && numberOfItemsToScan != -1) {
//                break;
//            }
//            callables.add(new AdScraper(element, this));
//            i++;
//        }
//        try {
//            advertisementFutureList = executorService.invokeAll(callables);
//            for (Future<Advertisement> advertisementFuture : advertisementFutureList) {
//                advertisementList.add(advertisementFuture.get());
//            }
//        } catch (InterruptedException | ExecutionException e) {
//            e.printStackTrace();
//        }
//
//        return advertisementList;
//    }
//
//    public Advertisement getAdvertisementFromLink(String link) throws IOException {
//        HtmlPage htmlPage = getNewHTMLPage(link);
//        //Click show cell number and name anchor
//        String originalAdvertisementId = getOriginalIdFromLink(link);
//        DomElement elementById = htmlPage.getElementById("ancPhoneNumber_" + originalAdvertisementId);
//        HtmlAnchor showCellNumberAnchor = (HtmlAnchor) elementById;
//        htmlPage = showCellNumberAnchor.click();
//        waitForScript(200);
//
//        Document doc = Jsoup.parse(htmlPage.asXml());
//        //.FRight:not([id])
//        Elements textElements = doc.select(".Data");
//        Elements imgElements = doc.select(".FLeft");
//        Elements priceElements = doc.select(".PriceData");
//        Elements extraInfoElements = doc.select(".ExNoteInfo");
//        Element contactNameElement = doc.getElementById("ctl00_PageContentPlaceHolder_innerAdContent_innerAdContact_lblContactName");
//        Element phoneNumberElement = doc.getElementById("spnPhoneNumber_" + originalAdvertisementId);
//        Element phoneNumber2Element = doc.getElementById("spnPhone2_" + originalAdvertisementId);
//
//        Advertisement advertisement = new Advertisement();
//        advertisement.setSite(getSiteURL());
//        advertisement.setOriginalId(originalAdvertisementId);
//        advertisement.setPropertyType(getElementValue(textElements.get(0)));
//        advertisement.setSubarea(getElementValue(textElements.get(1)));
//        advertisement.setCity(getElementValue(textElements.get(2)));
//        advertisement.setNeighborhood(getElementValue(textElements.get(3)));
//        advertisement.setAddress(getElementValue(textElements.get(4)));
//        advertisement.setRoom(getElementValue(textElements.get(5)));
//        advertisement.setFloor(getElementValue(textElements.get(6)));//Need to roll out child nodes and concatenate Element html
//        advertisement.setStatus(getElementValue(textElements.get(7)));
//        advertisement.setAvailableDate(getElementValue(textElements.get(8)));//"Login" field
//        advertisement.setSquareMetersSize(getElementValue(textElements.get(9)));//Maybe! Need to roll out child nodes and concatenate Element html
//
//        advertisement.setPrice(StringUtils.trim(priceElements.get(0).childNodes().get(2).toString()));
//
//        advertisement.setParking(isAvailable(imgElements.get(0)));
//        advertisement.setAir(isAvailable(imgElements.get(1)));
//        advertisement.setSecurityRoom(isAvailable(imgElements.get(2)));
//        advertisement.setElevator(isAvailable(imgElements.get(3)));
//        advertisement.setHasBalcony(isAvailable(imgElements.get(4)));
//        advertisement.setAccessForDisabled(isAvailable(imgElements.get(5)));
//        //advertisement.setAccessForDisabled(isAvailable(imgElements.get(6)));// What is דלתות פנדור?
//        advertisement.setExtras(getElementValue(extraInfoElements.get(0)));
//        advertisement.setUserName(contactNameElement.html());
//        if (phoneNumberElement != null && phoneNumber2Element != null) {
//            advertisement.setUserPhone(phoneNumberElement.html());
//            advertisement.setUserPhone2(phoneNumber2Element.html());
//        }
//
//        return advertisement;
//    }
//
//    private HtmlPage getNewHTMLPage(String link) throws IOException {
//        return new WebClient().getPage(link);
//    }
//
//    private boolean isAvailable(Element element) {
//        return !element.select(".VImage").isEmpty();
//    }
//
//    private String getElementValue(Element element) {
//        return element.child(0).html();
//    }
//
//    @Override
//    protected HtmlPage getPage(int pageNumber, String baseURI) throws IOException {
//        String delimiter = getParametersDelimiter(baseURI);
//        return getHTMLPage(String.format("%s%sPageNumberBottom=%s&PageNumberTop=%s", baseURI, delimiter, pageNumber, pageNumber));
//    }
//
//    @Override
//    protected boolean hasNextPage(HtmlPage htmlPage) {
//        Document doc = Jsoup.parse(htmlPage.asXml());
//        return !doc.select(".pagingArrowRight").isEmpty() && doc.select(".NoResults").isEmpty();
//    }
//
//    @Override
//    protected boolean isProcessCurrentPage(HtmlPage htmlPage) {
//        return Jsoup.parse(htmlPage.asXml()).select(".NoResults").isEmpty();
//    }
//
//    @Override
//    protected HtmlPage applyFilters(HtmlPage htmlPage, AdvertisementFilter filter) {
//        return htmlPage;
//    }
//
//    @Override
//    public String getSiteURL() {
//        return "http://www.winwin.co.il/RealEstate/ForSale/RealEstatePage.aspx";
//    }
//
//    public String getOriginalIdFromLink(String link) {
//        int beginIndex = link.indexOf("ObjId=") + 6;
//        return link.substring(beginIndex, link.indexOf("&", beginIndex));
//    }
}
//
//http://www.winwin.co.il/RealEstate/ForSale/RealEstatePage.aspx?PageNumberBottom=2&PageNumberTop=2
//http://www.winwin.co.il/Pages/RealEstatePages/RealEstateSaleGridTemplate.aspx?NsId=254&ObjId=4199160&boneid=599&ItemIndex=0&Menu=1&Personal=False&rnd=100